{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d9ae8a50-8d2e-4480-9174-dea26bfeada6",
   "metadata": {},
   "source": [
    "# LlamaHub Demostration\n",
    "\n",
    "Here we give a simple overview of how to use data loaders and tools (for agents) within [LlamaHub](llamahub.ai).\n",
    "\n",
    "**NOTES**: \n",
    "\n",
    "- You can learn how to use everything in LlamaHub by clicking into each module and looking at the code snippet.\n",
    "- Also, you can find a [full list of agent tools here](https://llamahub.ai/?tab=tools).\n",
    "- In this guide we'll show how to use `download_loader` and `download_tool`. You can also install `llama-hub` [as a package](https://github.com/run-llama/llama-hub#usage-use-llama-hub-as-pypi-package).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "968ce1de-cbd8-41ab-93b4-64a5dd824ac8",
   "metadata": {},
   "source": [
    "## Using a Data Loader\n",
    "\n",
    "In this example we show how to use `SimpleWebPageReader`.\n",
    "\n",
    "**NOTE**: for any module on LlamaHub, to use with `download_` functions, note down the class name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ee6aa991",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-agent-openai\n",
    "%pip install llama-index-readers-web\n",
    "%pip install llama-index-tools-google"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d76748f4-5aaf-482e-9a8b-e21ddd067ff5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.readers.web import SimpleWebPageReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78511163-8100-4e7a-bc52-09ba64dbb4d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "reader = SimpleWebPageReader(html_to_text=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a452ae1f-df10-4029-afe1-60497e229d1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "docs = reader.load_data(urls=[\"https://eugeneyan.com/writing/llm-patterns/\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a8438799-bdd6-4c41-b692-b6954add4036",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# [eugeneyan](/)\n",
      "\n",
      "  * [Start Here](/start-here/ \"Start Here\")\n",
      "  * [Writing](/writing/ \"Writing\")\n",
      "  * [Speaking](/speaking/ \"Speaking\")\n",
      "  * [Prototyping](/prototyping/ \"Prototyping\")\n",
      "  * [About](/about/ \"About\")\n",
      "\n",
      "# Patterns for Building LLM-based Systems & Products\n",
      "\n",
      "[ [llm](/tag/llm/) [engineering](/tag/engineering/)\n",
      "[production](/tag/production/) ]  · 66 min read\n",
      "\n",
      "> Discussions on [HackerNews](htt\n"
     ]
    }
   ],
   "source": [
    "print(docs[0].get_content()[:400])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aaf9f5fe-c5be-4a47-ba89-d428958ac6e6",
   "metadata": {},
   "source": [
    "Now you can plug these docs into your downstream LlamaIndex pipeline."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff5dd6bb-0d06-4a12-a71c-e7c6de20161b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core import VectorStoreIndex\n",
    "\n",
    "index = VectorStoreIndex.from_documents(docs)\n",
    "query_engine = index.as_query_engine()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d113f00e-01ae-4d14-a06f-136f7625db1e",
   "metadata": {},
   "outputs": [],
   "source": [
    "response = query_engine.query(\"What are ways to evaluate LLMs?\")\n",
    "print(str(response))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1493f759-491f-4501-bcf5-07c1a3324ff0",
   "metadata": {},
   "source": [
    "## Using an Agent Tool Spec\n",
    "\n",
    "In this example we show how to load an agent tool."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cb2bd887-d282-4651-b06f-884bdfcf72e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.tools.google import GmailToolSpec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "445cb5d8-0a37-49cd-b101-4e61a8a53fe4",
   "metadata": {},
   "outputs": [],
   "source": [
    "tool_spec = GmailToolSpec()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "95f2ae2e-ade3-4158-ba78-4ce33938ee4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# plug into your agent\n",
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.llms.openai import OpenAI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96d9eed7-98ba-470f-a513-ba54a6b8b09c",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = FunctionAgent(\n",
    "    tools=tool_spec.to_tool_list(),\n",
    "    llm=OpenAI(model=\"gpt-4.1-mini\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01be9bd7-f3c5-4a7c-89ad-62c3a34fcfce",
   "metadata": {},
   "outputs": [],
   "source": [
    "await agent.run(\"What is my most recent email\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama_index_v2",
   "language": "python",
   "name": "llama_index_v2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
